package caidan;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;

import jinyilw.swing.SwingTools;
import jinyilw.swing.rlaf.utils.RLafUtils;
import jinyilw.swing.visuals.Rcomp.RDialog;

public class SetMoreDialog extends RDialog
{
	private static final long serialVersionUID = -5358851456077328170L;
	private JTextField appNameField, userNameField;
	private JSpinner maxRowSpinner;
	private SpinnerNumberModel maxRowModel;
	private JButton saveAndRestart;
	private MainFrame mainFrame;

	public SetMoreDialog(JDialog dialog, MainFrame frame)
	{
		super(dialog);
		mainFrame = frame;
		setOptionStyle(150, 0.95f);
		setSection(12);
		setTitleHeight(28);
		setTitle("更多设置");
		setTitleColor(Color.green);
		appNameField = new JTextField(18);
		userNameField = new JTextField(18);
		maxRowModel = new SpinnerNumberModel(MainApp.maxRow, 8, 15, 1);
		maxRowSpinner = new JSpinner(maxRowModel);
		SwingTools.makeSpinnerTextCenter(maxRowSpinner);
		Container container = getContentPane();
		Container setContainer = new Container();
		Container buttonContainer = new JPanel();
		container.add(setContainer, BorderLayout.CENTER);
		container.add(buttonContainer, BorderLayout.SOUTH);
		setContainer.setLayout(new GridLayout(0, 1, 0, 2));

		Container setAppNamePane = new Container();
		setAppNamePane.setLayout(new FlowLayout(FlowLayout.LEFT));
		setAppNamePane.add(new JLabel("软件名："));
		setAppNamePane.add(appNameField);
		setContainer.add(setAppNamePane);

		Container setUserNamePane = new Container();
		setUserNamePane.setLayout(new FlowLayout(FlowLayout.LEFT));
		setUserNamePane.add(new JLabel("用户名："));
		setUserNamePane.add(userNameField);
		setContainer.add(setUserNamePane);

		Container setMaxRowPane = new Container();
		setMaxRowPane.setLayout(new FlowLayout(FlowLayout.LEFT));
		setMaxRowPane.add(new JLabel("行   数："));
		setMaxRowPane.add(maxRowSpinner);
		setContainer.add(setMaxRowPane);

		JButton reset = new JButton("默认值");
		buttonContainer.add(reset);
		reset.addActionListener((ActionEvent e) ->
		{
			appNameField.setText(MainApp.APP_NAME);
			maxRowModel.setValue(MainApp.MAX_ROW);
		});

		JButton save = new JButton("保存");
		buttonContainer.add(save);
		save.addActionListener((ActionEvent e) ->
		{
			save();
		});
		saveAndRestart = new JButton("保存并重启");
		buttonContainer.add(saveAndRestart);
		saveAndRestart.addActionListener((ActionEvent e) ->
		{
			save();
			RLafUtils.restartApplication();
		});
		appNameField.addActionListener((ActionEvent e) ->
		{
			String appName = appNameField.getText();
			if (!MainApp.appName.equals(appName))
			{
				MainApp.setAppName(appName);
				mainFrame.updateTitle();
			}
		});
		userNameField.addActionListener((ActionEvent e) ->
		{
			String userName = userNameField.getText();
			if (!MainApp.userName.equals(userName))
			{
				MainApp.setUserName(userName);
				mainFrame.sendUserInfo();
			}
		});
		maxRowSpinner.addChangeListener((ChangeEvent e) ->
		{
			int maxRow = (int) maxRowModel.getNumber();
			if (MainApp.maxRow != maxRow)
				saveAndRestart.setEnabled(true);
			else
				saveAndRestart.setEnabled(false);
		});
		setResizable(false);
		pack();
	}

	public void save()
	{
		String appName = appNameField.getText();
		if (!MainApp.appName.equals(appName))
		{
			MainApp.setAppName(appName);
			mainFrame.updateTitle();
		}
		String userName = userNameField.getText();
		if (!MainApp.userName.equals(userName))
		{
			MainApp.setUserName(userName);
			mainFrame.sendUserInfo();
		}
		int maxRow = (int) maxRowModel.getNumber();
		if (MainApp.maxRow != maxRow && maxRow >= 7 && maxRow <= 15)
			MainApp.setMaxRow(maxRow);
	}

	public void updateContent()
	{
		appNameField.setText(MainApp.appName);
		userNameField.setText(MainApp.userName);
		maxRowModel.setValue(MainApp.maxRow);
		saveAndRestart.setEnabled(false);
	}
}
